package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.model.PaasFormDataEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.domain.dao.ExpenseDao;
import com.xbongbong.saas.domain.entity.ext.ExpenseEntityExt;
import com.xbongbong.saas.model.ExpenseModel;
import com.xbongbong.saas.util.StandardizeDataUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报销单
 * 创建时间 2020/3/14 4:49 PM
 * 修改时间 2020/3/14 4:49 PM
 * @author chy
 */
@Service("expenseModel")
public class ExpenseModelImpl implements ExpenseModel {

    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private ExpenseDao expenseDao;
    @Resource
    private PaasFormDataEsModel paasFormDataEsModel;

    @Override
    public Integer insert(ExpenseEntityExt entity) throws XbbException {
        return null;
    }

    @Override
    public Integer update(ExpenseEntityExt entity) throws XbbException {
        return null;
    }

    @Override
    public List findEntitys(Map<String, Object> param) {
        return expenseDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return expenseDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public void insertBatch(List<ExpenseEntityExt> expenseEntityExtList) throws XbbException {
        StandardizeDataUtil.standardize4EntityList(expenseEntityExtList);
        expenseDao.insertBatch(expenseEntityExtList);
        //ES数据插入
        List<InsertDTO> insertList = new ArrayList<>();
        for (ExpenseEntityExt item : expenseEntityExtList) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(item.getCorpid() + "_" + item.getId());
            item.setDataId(item.getId());
            insertDTO.setSource(BeanUtil.convertBean2Map(item, true, true));
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_EXPENSE);
            insertList.add(insertDTO);
        }
        paasEsModel.insertBatch(insertList);
    }

    @Override
    public ExpenseEntityExt getByKey(Long key, String corpid) {
        return expenseDao.getByKey(key, corpid);
    }

    @Override
    public Integer delete(Long key, String corpid) {
        return null;
    }

    @Override
    public Integer deleteBatch(List<Long> idIn, String corpid) throws XbbException {
        Integer integer = expenseDao.deleteBatch(idIn, corpid);
        paasEsModel.deleteBatch(idIn, corpid, DelEnum.DELETE.getDel(), IndexTypeEnum.IDX_SAAS_EXPENSE);
        return integer;
    }

    private void updateBatchDel(List<Long> dataIdList, String corpid, Integer del) throws XbbException{
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("del", del);
        map.put("updateTime", DateTimeUtil.getInt());
        paasFormDataEsModel.updateBatch(dataIdList, corpid, map, IndexTypeEnum.IDX_SAAS_EXPENSE);
    }
}
